import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:inmotion_pro/pages/others/custom_view/others/custom_switch.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

// 设置项基类
class SettingRow extends StatelessWidget {
  final String title;
  final String? subtitle;
  final String? value;
  final bool? switchValue;
  final Widget? leading;
  final Widget? trailing;
  final Function()? onTap;
  final Function(bool)? switchOnChanged;
  final EdgeInsetsGeometry? padding;
  final List<Widget>? children;
  final bool isLastRow;
  final bool isMainRow;

  const SettingRow({
    super.key,
    required this.title,
    this.value,
    this.switchValue,
    this.subtitle,
    this.leading,
    this.trailing,
    this.onTap,
    this.switchOnChanged,
    this.padding,
    this.children,
    this.isLastRow = false,
    this.isMainRow = true,
  });

  // 仅显示标题类型
  factory SettingRow.onlyTitle({
    required String title,
    String? subtitle,
    Widget? leading,
    Function()? onTap,
    EdgeInsetsGeometry? padding,
    List<Widget>? children,
    bool isLastRow = false,
    bool isMainRow = true,
  }) {
    return SettingRow(
      title: title,
      subtitle: subtitle,
      value: "",
      leading: leading,
      onTap: onTap,
      padding: padding,
      isLastRow: isLastRow,
      isMainRow: isMainRow,
      children: children,
    );
  }

  // 仅显示值类型
  factory SettingRow.onlyValue({
    required String title,
    String? subtitle,
    String? value,
    Widget? trailing,
    Widget? leading,
    Function()? onTap,
    EdgeInsetsGeometry? padding,
    List<Widget>? children,
    bool isLastRow = false,
    bool isMainRow = true,
  }) {
    return SettingRow(
      title: title,
      subtitle: subtitle,
      value: value,
      leading: leading,
      trailing: trailing,
      onTap: onTap,
      padding: padding,
      isLastRow: isLastRow,
      isMainRow: isMainRow,
      children: children,
    );
  }

  // 仅显示箭头
  factory SettingRow.onlyArrow({
    required String title,
    String? subtitle,
    Widget? leading,
    Widget? trailing,
    Function()? onTap,
    EdgeInsetsGeometry? padding,
    List<Widget>? children,
    bool isLastRow = false,
    bool isMainRow = true,
  }) {
    return SettingRow(
      title: title,
      subtitle: subtitle,
      leading: leading,
      trailing: trailing ??
          Icon(Icons.chevron_right,
              color: Colors.white.withOpacity(isMainRow ? 1 : 0.5)),
      onTap: onTap,
      padding: padding,
      isLastRow: isLastRow,
      isMainRow: isMainRow,
      children: children,
    );
  }

  // 显示箭头和值
  factory SettingRow.arrowWithValue({
    required String title,
    String? subtitle,
    Widget? trailing,
    String? value,
    Widget? leading,
    Function()? onTap,
    EdgeInsetsGeometry? padding,
    List<Widget>? children,
    bool isLastRow = false,
    bool isMainRow = true,
  }) {
    return SettingRow(
      title: title,
      subtitle: subtitle,
      value: value,
      leading: leading,
      trailing: Icon(Icons.chevron_right,
          color: Colors.white.withOpacity(isMainRow ? 1 : 0.5)),
      onTap: onTap,
      padding: padding,
      isLastRow: isLastRow,
      isMainRow: isMainRow,
      children: children,
    );
  }

  // 开关类型
  factory SettingRow.switchTile({
    required String title,
    String? subtitle,
    bool? switchValue,
    Widget? leading,
    Widget? trailing,
    Function(bool)? switchOnChanged,
    EdgeInsetsGeometry? padding,
    List<Widget>? children,
    bool isLastRow = false,
    bool isMainRow = true,
  }) {
    return SettingRow(
      title: title,
      subtitle: subtitle,
      switchValue: switchValue,
      leading: leading,
      trailing: trailing,
      switchOnChanged: switchOnChanged,
      padding: padding,
      isLastRow: isLastRow,
      isMainRow: isMainRow,
      children: children,
    );
  }

  @override
  Widget build(BuildContext context) {
    // 如果是Row是主行，并且没有子项
    bool isMainSingleRow = isMainRow &&
        (children == null || (children != null && children!.isEmpty));
    return Material(
      color: GrayColor.gray1,
      child: InkWell(
        onTap: onTap,
        child: Padding(
          padding: padding ??
              EdgeInsets.only(
                left: CONTENT_MARGIN,
                right: isMainRow ? CONTENT_MARGIN : 0,
                top: CONTENT_MARGIN,
                bottom: CONTENT_MARGIN / 2,
              ),
          child: Column(
            children: [
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  if (leading != null) leading!,
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          title,
                          style: TextStyle(
                              fontSize: isMainRow == true ? 16.sp : 16.sp,
                              fontWeight: FontWeight.w500,
                              color: isMainRow == true
                                  ? Colors.white
                                  : Colors.white.withOpacity(0.5)),
                          maxLines: 2,
                        ),
                        if (subtitle != null)
                          Padding(
                            padding: EdgeInsets.only(top: 4.h),
                            child: Text(subtitle!,
                                style: TextStyle(
                                    fontSize: 12.sp,
                                    color: Colors.white.withOpacity(0.5))),
                          ),
                      ],
                    ),
                  ),
                  if (value != null)
                    Expanded(
                      flex: 2, // 根据需要调整比例
                      child: Padding(
                        padding: EdgeInsets.only(left: 8.w, right: 2.w),
                        child: Text(
                          value!,
                          style: TextStyle(
                            fontSize: 14.sp,
                            color: Colors.grey[600],
                          ),
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          textAlign: TextAlign.right,
                        ),
                      ),
                    ),
                  if (switchValue != null)
                    SizedBox(
                      child: CustomSwitch(
                        value: switchValue!,
                        onChanged: switchOnChanged,
                      ),
                    ),
                  if (trailing != null) trailing!,
                ],
              ),

              SizedBox(height: 10.w),

              // children不为空时，switchValue为true时，显示子项
              // 【或】switchValue为true时，显示子项
              if ((children != null && switchValue != false) ||
                  switchValue == true) ...[
                ...children!.asMap().entries.map(
                  (entry) {
                    final isLast = entry.key == children!.length - 1;
                    return Column(
                      children: [
                        entry.value,
                        if (!(isLastRow && isLast))
                          Padding(
                            padding: EdgeInsets.only(
                                left: isLast ? 0 : CONTENT_MARGIN),
                            child: Divider(
                              height: 1,
                              color: Colors.white.withOpacity(0.1),
                            ),
                          ),
                      ],
                    );
                  },
                ),
              ],
              if (isMainSingleRow && !isLastRow)
                Padding(
                  padding: EdgeInsets.only(top: 8.h),
                  child: Divider(
                    height: 1,
                    color: Colors.white.withOpacity(0.1),
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }
}
